//商品状态机
const axios = require('axios')
module.exports = {
  namespaced: true,
  state: {
    commodity: [],
    several: 4,
    yeshu: 1,
    pages: '',
    rowss: '',
    lei: '',
    leirong: '',
  },
  mutations: {
    getcommodity(state, data) {
      state.commodity = data.data
      state.several = data.several
      state.yeshu = data.yeshu
      state.rowss = data.rowss
      state.pages = data.pages
    },
    xiaye(state) {
      if (state.yeshu < state.pages) {
        state.yeshu++
      }
    },
    shangye(state) {
      if (state.yeshu > 1) {
        state.yeshu--
      }
    },
    shou(state) {
      state.yeshu = 1
    },
    wei(state) {
      state.yeshu = state.pages
    },
    //查询
    cha(state, data) {
      state.lei = data.lei
      state.leirong = data.leirong
    },
  },
  actions: {
    getcommodityactions(context) {
      axios.get('/merchant/getcommodity', { params: { several: context.state.several, yeshu: context.state.yeshu, lei: context.state.lei, leirong: context.state.leirong } }).then(({ data }) => {
        // console.log('zhu',data)
        context.commit("getcommodity", data)
      })

    },
    //新增商品
    async addcommodity(context, data1) {
      var { data } = await axios.post('/merchant/addcommodity', { data1 })
      return data
    },
    //删除商品
    async deltcommodity(context, data1) {
      var { data } = await axios.get('/merchant/deltcommodity', { params: { _id: data1 } })
      return data
    },
    //显示修改商品
    async updatecommodity(context, data1) {
      var { data } = await axios.get('/merchant/updatecommodity', { params: { _id: data1 } })
      return data[0]
    },
    //修改商品
    async submitcommodity(context, data) {
      await axios.post('/merchant/submitcommodity', { data })
    },
    //查询商品
    async inquirecommodity(context, data1) {
      context.commit("cha", data1)
      context.dispatch('getcommodityactions')
      //  var {data}= await axios.get('/merchant/inquirecommodity',{params:data1})
      //  context.commit("getcommodity",data)
    },
    //下一页
    overleaf(context, data) {
      context.commit("xiaye")
      context.dispatch('getcommodityactions')
    },
    //上一页
    pageup(context, data) {
      context.commit("shangye")
      context.dispatch('getcommodityactions')
    },
    //首页
    head(context, data) {
      context.commit("shou")
      context.dispatch('getcommodityactions')
    },
    //尾页
    footer(context, data) {
      context.commit("wei")
      context.dispatch('getcommodityactions')
    }
  }
}